/**
 * 
 */
package com.ap.core.jbehave.manageconsumer;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.hamcrest.CoreMatchers;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import com.ap.core.cxf.jaxb.common.ConsumerStatusEnum;
import com.ap.core.cxf.jaxb.common.YNType;
import com.ap.core.cxf.jaxb.manageconsumer.RequestRetrieveConsumerDetailsType;
import com.ap.core.cxf.jaxb.manageconsumer.ResponseRetrieveConsumerDetailsType;
import com.ap.core.cxf.jaxb.msghdr.MessageType;
import com.ap.core.jbehave.BaseRTPSteps;
import com.ap.core.jbehave.StoryContext;
import com.ap.core.util.JaxbUtil;
import com.ap.generator.consumer.retrievedetails.RequestRetrieveConsumerDetailsTypeGeneratorBuilder;
import com.core.ap.wsdl.manageconsumerservice_1.ManageConsumer1PortType;
import com.core.ap.wsdl.manageconsumerservice_1.RetrieveConsumerDetails1Fault;

/**
 * The Class RetrieveConsumerDetailsSteps.
 *
 * @author Siva.Pochana
 */
public class RetrieveConsumerDetailsSteps extends BaseRTPSteps {

	/** The request retrieve consumer details type. */
	private RequestRetrieveConsumerDetailsType requestRetrieveConsumerDetailsType = null;
	
	/** The response retrieve consumer details type. */
	private ResponseRetrieveConsumerDetailsType responseRetrieveConsumerDetailsType = null;
	
	/**
	 * Instantiates a new retrieve consumer details steps.
	 *
	 * @param storyContext the story context
	 */
	public RetrieveConsumerDetailsSteps(StoryContext storyContext) {
		super(storyContext);
	}

	/**
	 * Give new zapp account.
	 */
	@Given("the Email Address is not being used by an Active Zapp account as a Username")
	public void giveNewZappAccount() {
		requestRetrieveConsumerDetailsType = RequestRetrieveConsumerDetailsTypeGeneratorBuilder.getRequestRetrieveConsumerDetailsTypeGenerator().build().generate();
		requestRetrieveConsumerDetailsType.getConsRegRequestInfo().setEmailId("abc_abc123@amail.com");
		requestRetrieveConsumerDetailsType.getConsRegRequestInfo().setFIConsumerId("ntwdcon1");
		requestRetrieveConsumerDetailsType.getConsRegRequestInfo().setFIId("000001");
		
		JaxbUtil.newInstance().javaToXML(RequestRetrieveConsumerDetailsType.class, requestRetrieveConsumerDetailsType);		
	}
	
	/**
	 * When valid reg request received.
	 *
	 * @throws RetrieveConsumerDetails1Fault the retrieve consumer details1 fault
	 */
	@When("the valid Consumer registration Status Request message is received from the Consumer's FI")
	public void whenValidRegRequestReceived() throws RetrieveConsumerDetails1Fault {
		final ManageConsumer1PortType manageConsumer1PortType = factory.getBean(ManageConsumer1PortType.class);
		responseRetrieveConsumerDetailsType = manageConsumer1PortType.retrieveConsumerDetails1(requestRetrieveConsumerDetailsType);
		JaxbUtil.newInstance().javaToXML(ResponseRetrieveConsumerDetailsType.class, responseRetrieveConsumerDetailsType);	
	}
	
	/**
	 * Then set consumer status.
	 */
	@Then("set the Consumer Status of the response to NotKnown")
	public void thenSetConsumerStatus() {
		 assertThat("Consumer Status is NULL", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestResponseInfo().getConsumerStatus() == ConsumerStatusEnum.NOTKNOWN)));
	}
	
	/**
	 * Return response.
	 */
	@Then("return the response back to the Consumer's FI")
	public void thenReturnResponse() {
		 assertThat("Message Type  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getMsgHdr().getMsgType() == MessageType.RESPCONSREGSTATUS)));
		 assertThat("Original message Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getMsgHdr().getOrgReqMsgId().equals(requestRetrieveConsumerDetailsType.getMsgHdr().getMsgId()))));
		 assertThat("Email Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestInfo().getEmailId().equals(requestRetrieveConsumerDetailsType.getConsRegRequestInfo().getEmailId()))));
		 assertThat("FI Consumer Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIConsumerId().equals(requestRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIConsumerId()))));
		 assertThat("FI Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIId().equals(requestRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIId()))));
	}
	
	@Given("the Email Address is being used by an Active Zapp account as a Username (there can be only one)")
	public void givenTheEmailIsActiveZappAct() {
		requestRetrieveConsumerDetailsType = RequestRetrieveConsumerDetailsTypeGeneratorBuilder.getRequestRetrieveConsumerDetailsTypeGenerator().build().generate();
		requestRetrieveConsumerDetailsType.getConsRegRequestInfo().setEmailId("abc_abc@amail.com");
		requestRetrieveConsumerDetailsType.getConsRegRequestInfo().setFIConsumerId("ntwdcon1");
		requestRetrieveConsumerDetailsType.getConsRegRequestInfo().setFIId("000001");
		
		JaxbUtil.newInstance().javaToXML(RequestRetrieveConsumerDetailsType.class, requestRetrieveConsumerDetailsType);			
	}
	
	@Given("a linked FI Consumer account does not exist for the submitting FIID")
	public void givenLinkedFIConsumerNotExists() {
	}
	
	@When("the valid Consumer registration Status Request message is received")
	public void whenValidRegStatusReceived() throws RetrieveConsumerDetails1Fault {
		final ManageConsumer1PortType manageConsumer1PortType = factory.getBean(ManageConsumer1PortType.class);
		responseRetrieveConsumerDetailsType = manageConsumer1PortType.retrieveConsumerDetails1(requestRetrieveConsumerDetailsType);
		JaxbUtil.newInstance().javaToXML(ResponseRetrieveConsumerDetailsType.class, responseRetrieveConsumerDetailsType);			
	}
	
	@Then("set the Consumer Status of the response to the state of the Zapp account (which in this scenario will be Active)")
	public void thenSetConsumerStatusInResponse() {
		 assertThat("Message Type  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getMsgHdr().getMsgType() == MessageType.RESPCONSREGSTATUS)));
		 assertThat("Original message Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getMsgHdr().getOrgReqMsgId().equals(requestRetrieveConsumerDetailsType.getMsgHdr().getMsgId()))));
		 assertThat("Email Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestInfo().getEmailId().equals(requestRetrieveConsumerDetailsType.getConsRegRequestInfo().getEmailId()))));
		 assertThat("FI Consumer Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIConsumerId().equals(requestRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIConsumerId()))));
		 assertThat("FI Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIId().equals(requestRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIId()))));
		
	}
	
	@Then("set the Mobile Number of the response to the Mobile Number of the Zapp account")
	public void thenSetTheMobileNum() {
		assertThat("Consumer Mobile Number is NULL", false, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestResponseInfo().getMobNb() == null)));
	}
	
	@Then("set the FI Consumer Flag of the response to N")
	public void thenSetFIConsumerFlag() {
		assertThat("FI Consumer Status Flag is NULL", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestResponseInfo().getFIConsumerFlag() == YNType.N)));
	}

	/* ----- */
/*	@Given("the Email Address is being used by an Active Zapp account as a Username")
	public void givenActiveZappAct() {
		requestRetrieveConsumerDetailsType = RequestRetrieveConsumerDetailsTypeGeneratorBuilder.getRequestRetrieveConsumerDetailsTypeGenerator().build().generate();
		requestRetrieveConsumerDetailsType.getConsRegRequestInfo().setEmailId("abc_abc@amail.com");
		requestRetrieveConsumerDetailsType.getConsRegRequestInfo().setFIConsumerId("hsbccon4");
		requestRetrieveConsumerDetailsType.getConsRegRequestInfo().setFIId("000001");
		
		JaxbUtil.newInstance().javaToXML(RequestRetrieveConsumerDetailsType.class, requestRetrieveConsumerDetailsType);			
	}
	
	@Given("a linked FI Consumer account exists for the submitting FIID in an Active state")
	public void givenLinkedFIConsumerExists() {
		
	}
	
	@When("the valid Consumer registration Status Request message is received from the Consumer")
	public void whenValidRegStatusRequestArraive() throws RetrieveConsumerDetails1Fault {
		final ManageConsumer1PortType manageConsumer1PortType = factory.getBean(ManageConsumer1PortType.class);
		responseRetrieveConsumerDetailsType = manageConsumer1PortType.retrieveConsumerDetails1(requestRetrieveConsumerDetailsType);
		JaxbUtil.newInstance().javaToXML(ResponseRetrieveConsumerDetailsType.class, responseRetrieveConsumerDetailsType);			
	}
	
	@Then("set the Consumer Status of the response to the state of the Zapp account")
	public void thenSetConsumerStatusState() {
		 assertThat("Message Type  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getMsgHdr().getMsgType() == MessageType.RESPCONSREGSTATUS)));
		 assertThat("Original message Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getMsgHdr().getOrgReqMsgId().equals(requestRetrieveConsumerDetailsType.getMsgHdr().getMsgId()))));
		 assertThat("Email Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestInfo().getEmailId().equals(requestRetrieveConsumerDetailsType.getConsRegRequestInfo().getEmailId()))));
		 assertThat("FI Consumer Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIConsumerId().equals(requestRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIConsumerId()))));
		 assertThat("FI Id  is not as expected", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIId().equals(requestRetrieveConsumerDetailsType.getConsRegRequestInfo().getFIId()))));
	}
	
	@Then("set the Mobile Number into the response") 
	public void thenSetTheMobileNumberToResponse() {
		assertThat("Consumer Mobile Number is NULL", false, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestResponseInfo().getMobNb() == null)));
	}
	
	@Then("set the FI Consumer Flag of the response to Y") 
	public void thenSetTheFIConsumerFlag() {
		assertThat("FI Consumer Status Flag is NULL", true, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestResponseInfo().getFIConsumerFlag() == YNType.Y)));
	}
	
	@Then("set the HeldFIConsumerID of the response to the FIConsumerID of the linked Active FI Consumer account for the submitting FIID")
	public void thenSetTheHeldFIConsumerId() {
		assertThat("Held FI Consumer Id is NULL", false, CoreMatchers.is(equalTo(responseRetrieveConsumerDetailsType.getConsRegRequestResponseInfo().getHeldFIConsumerId() == null)));
	}*/
}
